Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): Memoize simple types without arguments #457

Merged
merged 1 commit into from
Apr 4, 2024

Conversation

veewee
Copy link
Collaborator

@veewee veewee commented Apr 3, 2024

Benchmarks (with --warmup=20 --revs=200 --retry-threshold=1)

The result there is that it has close to zero impact (+ or - 5% which is a matter of nanoseconds) on the regular types, but has a big impact on the shape() coercions.

The ShapeTypeBench is the only one that calls multiple types that now get memoized so it represents real-life wins better : In real applications, types tend to repeat more than in the benchmarks.

\Psl\Tests\Benchmark\Channel\CommunicationBench

    benchBoundedCommunication...............R1 I2 - [Mo2.436ms vs. Mo2.443ms] -0.29% (±0.71%)
    benchUnboundedCommunication.............R2 I3 - [Mo2.454ms vs. Mo2.444ms] +0.38% (±0.43%)

\Psl\Tests\Benchmark\Type\VecTypeBench

    benchCoerce # mixed, empty array........R2 I4 - [Mo0.145μs vs. Mo0.145μs] 0.00% (±0.00%)
    benchCoerce # mixed, empty iterable.....R5 I4 - [Mo0.180μs vs. Mo0.180μs] 0.00% (±0.00%)
    benchCoerce # mixed, non-empty array....R1 I0 - [Mo0.330μs vs. Mo0.330μs] -0.05% (±0.00%)
    benchCoerce # mixed, non-empty iterable.R5 I4 - [Mo0.460μs vs. Mo0.495μs] -7.07% (±0.44%)
    benchCoerce # mixed, large array........R2 I1 - [Mo5.276μs vs. Mo5.270μs] +0.12% (±0.55%)
    benchCoerce # mixed, large iterable.....R5 I4 - [Mo8.207μs vs. Mo8.198μs] +0.11% (±0.47%)
    benchCoerce # int, empty array..........R1 I3 - [Mo0.145μs vs. Mo0.145μs] 0.00% (±0.00%)
    benchCoerce # int, empty iterable.......R5 I4 - [Mo0.180μs vs. Mo0.180μs] 0.00% (±0.00%)
    benchCoerce # int, non-empty array......R5 I4 - [Mo0.360μs vs. Mo0.360μs] -0.05% (±0.68%)
    benchCoerce # int, non-empty iterable...R2 I4 - [Mo0.535μs vs. Mo0.535μs] +0.03% (±0.46%)
    benchAssert # mixed, empty array........R1 I1 - [Mo0.150μs vs. Mo0.150μs] 0.00% (±0.00%)
    benchAssert # mixed, non-empty array....R2 I4 - [Mo0.325μs vs. Mo0.325μs] 0.00% (±0.76%)
    benchAssert # mixed, large array........R1 I4 - [Mo4.886μs vs. Mo4.815μs] +1.48% (±0.32%)
    benchAssert # int, empty array..........R1 I4 - [Mo0.150μs vs. Mo0.150μs] 0.00% (±0.00%)
    benchAssert # int, non-empty array......R5 I4 - [Mo0.335μs vs. Mo0.340μs] -1.47% (±0.74%)
    benchMatch # mixed, empty array.........R5 I4 - [Mo0.125μs vs. Mo0.125μs] 0.00% (±0.00%)
    benchMatch # mixed, non-empty array.....R1 I1 - [Mo0.280μs vs. Mo0.280μs] 0.00% (±0.00%)
    benchMatch # mixed, large array.........R1 I3 - [Mo4.458μs vs. Mo4.544μs] -1.90% (±0.54%)
    benchMatch # int, empty array...........R5 I4 - [Mo0.125μs vs. Mo0.125μs] 0.00% (±0.00%)
    benchMatch # int, non-empty array.......R1 I4 - [Mo0.290μs vs. Mo0.285μs] +1.75% (±0.00%)

\Psl\Tests\Benchmark\Type\DictTypeBench

    benchCoerce # generic array, empty arra.R1 I2 - [Mo0.155μs vs. Mo0.150μs] +3.33% (±0.00%)
    benchCoerce # generic array, empty iter.R1 I3 - [Mo0.190μs vs. Mo0.190μs] 0.00% (±0.00%)
    benchCoerce # generic array, non-empty .R3 I4 - [Mo0.320μs vs. Mo0.315μs] +1.48% (±0.77%)
    benchCoerce # generic array, non-empty .R1 I4 - [Mo0.390μs vs. Mo0.390μs] 0.00% (±0.00%)
    benchCoerce # generic array, large arra.R1 I2 - [Mo12.696μs vs. Mo12.751μs] -0.43% (±0.51%)
    benchCoerce # generic array, large iter.R1 I0 - [Mo15.458μs vs. Mo15.728μs] -1.72% (±0.53%)
    benchCoerce # int array, empty array....R1 I1 - [Mo0.150μs vs. Mo0.155μs] -3.23% (±0.00%)
    benchCoerce # int array, empty iterable.R1 I4 - [Mo0.190μs vs. Mo0.190μs] 0.00% (±0.00%)
    benchCoerce # int array, non-empty arra.R1 I1 - [Mo0.510μs vs. Mo0.510μs] +0.00% (±0.00%)
    benchCoerce # int array, non-empty iter.R3 I4 - [Mo0.650μs vs. Mo0.645μs] +0.78% (±0.49%)
    benchCoerce # int array, large array....R1 I1 - [Mo10.082μs vs. Mo10.296μs] -2.07% (±0.40%)
    benchCoerce # int array, large iterable.R1 I3 - [Mo13.298μs vs. Mo13.517μs] -1.62% (±0.38%)
    benchCoerce # map, empty array..........R1 I4 - [Mo0.155μs vs. Mo0.155μs] 0.00% (±0.00%)
    benchCoerce # map, empty iterable.......R2 I2 - [Mo0.190μs vs. Mo0.190μs] 0.00% (±0.00%)
    benchCoerce # map, non-empty array......R1 I2 - [Mo0.590μs vs. Mo0.590μs] +0.03% (±0.41%)
    benchCoerce # map, non-empty iterable...R3 I3 - [Mo0.725μs vs. Mo0.720μs] +0.74% (±0.67%)
    benchCoerce # map, large array..........R1 I0 - [Mo10.702μs vs. Mo10.942μs] -2.20% (±0.45%)
    benchCoerce # map, large iterable.......R2 I2 - [Mo13.842μs vs. Mo13.947μs] -0.76% (±0.51%)
    benchAssert # generic array, empty arra.R1 I1 - [Mo0.140μs vs. Mo0.140μs] 0.00% (±0.00%)
    benchAssert # generic array, non-empty .R1 I4 - [Mo0.300μs vs. Mo0.290μs] +3.45% (±0.00%)
    benchAssert # generic array, large arra.R2 I3 - [Mo12.197μs vs. Mo11.986μs] +1.76% (±0.62%)
    benchAssert # int array, empty array....R1 I4 - [Mo0.140μs vs. Mo0.140μs] 0.00% (±0.00%)
    benchAssert # int array, non-empty arra.R5 I4 - [Mo0.460μs vs. Mo0.460μs] 0.00% (±0.53%)
    benchAssert # int array, large array....R5 I4 - [Mo10.717μs vs. Mo11.005μs] -2.62% (±0.56%)
    benchAssert # map, empty array..........R1 I1 - [Mo0.140μs vs. Mo0.140μs] 0.00% (±0.00%)
    benchAssert # map, non-empty array......R4 I4 - [Mo0.565μs vs. Mo0.565μs] +0.03% (±0.56%)
    benchAssert # map, large array..........R1 I2 - [Mo10.271μs vs. Mo10.456μs] -1.76% (±0.24%)
    benchMatch # generic array, empty array.R3 I4 - [Mo0.170μs vs. Mo0.170μs] 0.00% (±0.00%)
    benchMatch # generic array, non-empty a.R3 I3 - [Mo0.315μs vs. Mo0.320μs] -1.51% (±0.77%)
    benchMatch # generic array, large array.R5 I4 - [Mo11.921μs vs. Mo12.060μs] -1.15% (±0.35%)
    benchMatch # int array, empty array.....R1 I3 - [Mo0.170μs vs. Mo0.170μs] 0.00% (±0.00%)
    benchMatch # int array, non-empty array.R1 I3 - [Mo0.490μs vs. Mo0.495μs] -0.98% (±0.50%)
    benchMatch # int array, large array.....R2 I4 - [Mo10.918μs vs. Mo11.049μs] -1.18% (±0.49%)
    benchMatch # map, empty array...........R1 I1 - [Mo0.170μs vs. Mo0.170μs] 0.00% (±0.00%)
    benchMatch # map, non-empty array.......R5 I4 - [Mo0.585μs vs. Mo0.590μs] -0.82% (±0.42%)
    benchMatch # map, large array...........R3 I4 - [Mo10.247μs vs. Mo10.405μs] -1.52% (±0.72%)

\Psl\Tests\Benchmark\Type\NonEmptyStringTypeBench

    benchCoerce # string....................R1 I1 - [Mo0.105μs vs. Mo0.105μs] 0.00% (±0.00%)
    benchCoerce # int.......................R1 I0 - [Mo0.125μs vs. Mo0.125μs] 0.00% (±0.00%)
    benchCoerce # instanceof Stringable (ex.R5 I4 - [Mo0.150μs vs. Mo0.150μs] 0.00% (±0.00%)
    benchCoerce # instanceof Stringable (im.R1 I1 - [Mo0.155μs vs. Mo0.150μs] +3.33% (±0.00%)
    benchAssert.............................R1 I1 - [Mo0.100μs vs. Mo0.100μs] 0.00% (±0.00%)
    benchMatch..............................R1 I3 - [Mo0.105μs vs. Mo0.105μs] 0.00% (±0.00%)

\Psl\Tests\Benchmark\Type\StringTypeBench

    benchCoerce # string....................R2 I4 - [Mo0.090μs vs. Mo0.090μs] 0.00% (±0.00%)
    benchCoerce # int.......................R1 I4 - [Mo0.115μs vs. Mo0.120μs] -4.17% (±0.00%)
    benchCoerce # instanceof Stringable (ex.R1 I1 - [Mo0.140μs vs. Mo0.140μs] 0.00% (±0.00%)
    benchCoerce # instanceof Stringable (im.R3 I3 - [Mo0.140μs vs. Mo0.140μs] 0.00% (±0.00%)
    benchAssert.............................R5 I4 - [Mo0.085μs vs. Mo0.085μs] 0.00% (±0.00%)
    benchMatch..............................R5 I4 - [Mo0.100μs vs. Mo0.095μs] +5.26% (±0.00%)

\Psl\Tests\Benchmark\Type\ArrayKeyTypeBench

    benchCoerce # string....................R2 I4 - [Mo0.095μs vs. Mo0.095μs] 0.00% (±0.00%)
    benchCoerce # int.......................R5 I4 - [Mo0.100μs vs. Mo0.100μs] 0.00% (±0.00%)
    benchCoerce # instanceof Stringable (ex.R1 I3 - [Mo5.833μs vs. Mo5.729μs] +1.80% (±0.34%)
    benchCoerce # instanceof Stringable (im.R2 I3 - [Mo5.672μs vs. Mo5.802μs] -2.24% (±0.54%)
    benchAssert # string....................R4 I4 - [Mo0.090μs vs. Mo0.095μs] -5.26% (±0.00%)
    benchAssert # int.......................R5 I4 - [Mo0.100μs vs. Mo0.100μs] 0.00% (±0.00%)
    benchMatch # string.....................R5 I4 - [Mo0.100μs vs. Mo0.100μs] 0.00% (±0.00%)
    benchMatch # int........................R5 I4 - [Mo0.100μs vs. Mo0.100μs] 0.00% (±0.00%)

\Psl\Tests\Benchmark\Type\IntTypeBench

    benchCoerce # int.......................R2 I4 - [Mo0.095μs vs. Mo0.095μs] 0.00% (±0.00%)
    benchCoerce # string....................R1 I2 - [Mo0.165μs vs. Mo0.160μs] +3.12% (±0.00%)
    benchCoerce # float.....................R1 I3 - [Mo0.120μs vs. Mo0.120μs] 0.00% (±0.00%)
    benchCoerce # instanceof Stringable (ex.R1 I4 - [Mo0.205μs vs. Mo0.205μs] 0.00% (±0.00%)
    benchCoerce # instanceof Stringable (im.R1 I4 - [Mo0.205μs vs. Mo0.205μs] 0.00% (±0.00%)
    benchAssert.............................R2 I4 - [Mo0.085μs vs. Mo0.085μs] 0.00% (±0.00%)
    benchMatch..............................R5 I4 - [Mo0.095μs vs. Mo0.095μs] 0.00% (±0.00%)

\Psl\Tests\Benchmark\Type\ShapeTypeBench

    benchCoerce # empty shape, empty array .R4 I3 - [Mo0.335μs vs. Mo0.335μs] 0.00% (±0.74%)
    benchCoerce # empty shape, empty iterab.R1 I3 - [Mo0.315μs vs. Mo0.565μs] -44.23% (±0.00%)
    benchCoerce # empty shape, non-empty ar.R1 I4 - [Mo0.385μs vs. Mo0.370μs] +4.01% (±0.00%)
    benchCoerce # empty shape, non-empty it.R1 I1 - [Mo0.505μs vs. Mo0.772μs] -34.58% (±0.63%)
    benchCoerce # complex shape with option.R1 I4 - [Mo0.682μs vs. Mo0.683μs] -0.16% (±0.55%)
    benchCoerce # complex shape with option.R1 I4 - [Mo1.547μs vs. Mo1.806μs] -14.33% (±0.38%)
    benchCoerce # complex shape with option.R1 I0 - [Mo0.974μs vs. Mo0.993μs] -1.85% (±0.41%)
    benchCoerce # complex shape with option.R3 I4 - [Mo3.072μs vs. Mo3.367μs] -8.77% (±0.58%)
    benchAssert # empty shape, empty array .R1 I3 - [Mo0.130μs vs. Mo0.130μs] 0.00% (±0.00%)
    benchAssert # empty shape, non-empty ar.R1 I0 - [Mo0.230μs vs. Mo0.230μs] 0.00% (±0.00%)
    benchAssert # complex shape with option.R1 I3 - [Mo1.025μs vs. Mo1.027μs] -0.23% (±0.00%)
    benchAssert # complex shape with option.R1 I0 - [Mo2.102μs vs. Mo2.087μs] +0.69% (±0.41%)
    benchMatch # empty shape, empty array v.R1 I4 - [Mo0.160μs vs. Mo0.160μs] 0.00% (±0.00%)
    benchMatch # empty shape, non-empty arr.R4 I4 - [Mo0.260μs vs. Mo0.255μs] +1.96% (±0.00%)
    benchMatch # complex shape with optiona.R4 I4 - [Mo1.058μs vs. Mo1.060μs] -0.25% (±0.55%)
    benchMatch # complex shape with optiona.R2 I4 - [Mo2.135μs vs. Mo2.162μs] -1.28% (±0.23%)

@veewee veewee force-pushed the types-memozation branch from 0d1a808 to 27f9c63 Compare April 3, 2024 16:49
@@ -98,6 +98,58 @@ public function provideHappyPathCoercion(): array
'war' => null,
]),
],
'real-life-type-usage' => [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-04-03 at 18 48 48

The real-life-type-usage are rather limited

@coveralls
Copy link

coveralls commented Apr 3, 2024

Pull Request Test Coverage Report for Build 8550662461

Details

  • 63 of 63 (100.0%) changed or added relevant lines in 25 files are covered.
  • 6 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.1%) to 98.778%

Files with Coverage Reduction New Missed Lines %
src/Psl/Type/Internal/ArrayKeyType.php 1 88.89%
src/Psl/Type/Internal/NumType.php 1 50.0%
src/Psl/Type/Internal/ScalarType.php 4 20.0%
Totals Coverage Status
Change from base Build 8524527937: -0.1%
Covered Lines: 4365
Relevant Lines: 4419

💛 - Coveralls

@veewee veewee force-pushed the types-memozation branch from 27f9c63 to d96cdcc Compare April 4, 2024 06:18
@veewee veewee force-pushed the types-memozation branch from d96cdcc to 065b629 Compare April 4, 2024 07:08
@azjezz azjezz merged commit a4bd1e9 into azjezz:next Apr 4, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants